package cmfg;

import repast.simphony.context.Context;
import repast.simphony.context.space.continuous.ContinuousSpaceFactory;
import repast.simphony.context.space.continuous.ContinuousSpaceFactoryFinder;
import repast.simphony.context.space.graph.NetworkBuilder;
import repast.simphony.context.space.grid.GridFactory;
import repast.simphony.context.space.grid.GridFactoryFinder;
import repast.simphony.dataLoader.ContextBuilder;
import repast.simphony.engine.environment.RunEnvironment;
import repast.simphony.space.continuous.ContinuousSpace;
import repast.simphony.space.continuous.RandomCartesianAdder;
import repast.simphony.space.grid.Grid;
import repast.simphony.space.grid.GridBuilderParameters;
import repast.simphony.space.grid.SimpleGridAdder;
import repast.simphony.space.grid.WrapAroundBorders;

public class GridGoL implements ContextBuilder<Object> {

  @Override
	public Context build(Context<Object> context) {
		context.setId("Cmfg");
		Value value = (Value) RunEnvironment.getInstance().getParameters().getValue("value");
		NetDataServiceImpl NS = value.getNetDataService();
		String netType = value.getNetType();

		NetworkBuilder<Object> netBuilder1 = new NetworkBuilder<Object>("net", context, true);
		NetworkBuilder<Object> netBuilder2 = new NetworkBuilder<Object>("TSnet", context, false);
		NetworkBuilder<Object> netBuilder3 = new NetworkBuilder<Object>("Mnet", context, false);
		NetworkBuilder<Object> netBuilder4 = new NetworkBuilder<Object>("Pnet", context, false);
		netBuilder1.buildNetwork();
		netBuilder2.buildNetwork();
		netBuilder3.buildNetwork();
		netBuilder4.buildNetwork();
		GridFactory gridFactory;
		Grid<Object> grid;
		if (netType.equals("Grid")) {
			GridDataDaoImpl GridData = (GridDataDaoImpl) NS.getNetCLass();
			gridFactory = GridFactoryFinder.createGridFactory(null);
			grid = gridFactory.createGrid("grid", context, new GridBuilderParameters<Object>(new WrapAroundBorders(),
					new SimpleGridAdder<Object>(), false, GridData.getgridLength(), GridData.getgridWidth()));
		} else {
			gridFactory = GridFactoryFinder.createGridFactory(null);
			grid = gridFactory.createGrid("grid", context, new GridBuilderParameters<Object>(new WrapAroundBorders(),
					new SimpleGridAdder<Object>(), false, 100, 100));
		}

		ContinuousSpaceFactory spaceFactory = ContinuousSpaceFactoryFinder.createContinuousSpaceFactory(null);
		ContinuousSpace<Object> space = spaceFactory.createContinuousSpace("space", context,
				new RandomCartesianAdder<Object>(), new repast.simphony.space.continuous.WrapAroundBorders(),
				100, 100, 100);

		Initial init = new Initial();
		init.InitialNet(netType, context, space, grid);
        System.out.println("value:"+value.getUsername());
        
		return context;

	}
	
	

}
